from pyecharts import options as opts
from pyecharts.charts import Map, Geo, Bar, Line, Pie, WordCloud
from pyecharts.globals import ChartType
from pyecharts.options import ItemStyleOpts
from pyecharts.globals import ThemeType


colors = {
    "today_confirm":["white", "yellow", "orangered", "#C1232B"],
    "total_confirm":["white", "yellow", "orangered", "#C1232B"],
    "total_nowConfirm":["white", "yellow", "orangered", "#C1232B"],
    "total_suspect":["white", "blue", "#EC690C"],
    "total_dead" : ["white", "#4C4E80","#701380"],
    "total_heal" : ["white", "yellow", "green"],
    "total_deadRate" : ["white", "#4C4E80","#701380"],
    "total_healRate" : ["white", "yellow", "green"],
}

ma = {
    "today_confirm":50,
    "total_confirm":1200,
    "total_nowConfirm":100,
    "total_suspect":1200,
    "total_dead":1200,
    "total_heal":1200,
    "total_deadRate": 10,
    "total_healRate" : 100,
}



# update_time       total_confirm       today_confirm
# total_heal        today_heal          total_nowconfirm
# today_nowconfirm  total_dead          today_dead
# total_suspect     today_suspect
gt = {
    'update_time':'日期',
    'total_confirm':'累计确诊',
    'today_confirm':'当天确诊',
    'total_heal':'累计治愈',
    'today_heal':'当天治愈',
    'total_nowconfirm':'现存确诊',
    'today_nowconfirm':'今日现存确诊',
    'total_dead':'累计死亡',
    'today_dead':'当天死亡',
    'total_suspect':'累计疑似感染',
    'today_suspect':'当天新增疑似感染',
}


def china_pie(aa,tp) -> Pie:
    c=(
        Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add("",aa,
            radius=["40%", "75%"],
            label_opts=opts.LabelOpts(is_show=False),)
            .set_global_opts(
                title_opts=opts.TitleOpts(title="中国"+tp['usebase']+"饼状图"),
                legend_opts=opts.LegendOpts(is_show=True,type_ = 'scroll',pos_bottom = '0%',
                                            orient = 'horizontal',),
            )
            .set_series_opts()
    )
    c.render(path="D:\\javaweb\\pie.html")
    return c.dump_options_with_quotes()



def china_map(aa,tp) -> Map:
    c = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.DARK))
        #init_opts=opts.InitOpts(width="1400px", height="800px")
                #tp['usebase']
            .add(tp['usebase'], aa,
                 #tp.contry
                 "china",
                 is_roam=False,
                 is_map_symbol_show=False,
                 zoom=1.3,
                 itemstyle_opts=ItemStyleOpts(area_color="grey")
                 )
            .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            title_opts=opts.TitleOpts(title="中国"+tp['usebase']+"实况"),
            visualmap_opts=opts.VisualMapOpts(
                min_=0,
                max_=ma[tp['title']],
                range_text=["High", "Low"],
                is_calculable=True,
                            #colors[tp['name']]
                range_color=colors[tp['title']],
            ),
        )
    )
    c.render(path="D:\\javaweb\\map.html")
    return c.dump_options_with_quotes()

value_1 = [155, 10, 66]
attr_1 = ["成都市","绵阳市","泸州市"]
def province_map(aa,tp)->map:
    c=(
        Map()
        .add("品质类", [list(z) for z in zip(attr_1, value_1)],"四川")
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Map-VisualMap（连续型）"),
            visualmap_opts=opts.VisualMapOpts(max_=200,is_piecewise=True)
    )
    ).dump_options_with_quotes()
    return c


def china_wcd(aa,tp)->WordCloud:
    c = (
            WordCloud(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC))
            .add("", aa, word_size_range=[20, 50])
            .set_global_opts(title_opts=opts.TitleOpts(title="中国"+tp['usebase']+"词云"))
        )
    c.render(path="D:\\javaweb\\wordcloud.html")
    return c.dump_options_with_quotes()


def china_bar(aa,tp)-> Bar:
    c = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
        .add_xaxis([i[0] for i in aa])
        .add_yaxis(tp['usebase']+"/人", [i[1] for i in aa],
                   itemstyle_opts=opts.ItemStyleOpts(color=colors[tp['title']][-1]))
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        .set_global_opts(
            legend_opts=opts.LegendOpts(type_ = 'scroll', pos_bottom ='93%',),
            datazoom_opts=opts.DataZoomOpts(is_show= True,  is_realtime=True,
                                            range_start=40,range_end=100,orient= 'vertical', ),
            title_opts=opts.TitleOpts(title="中国"+tp['usebase']))
    )
    c.render(path="D:\\javaweb\\bar.html")
    return c.dump_options_with_quotes()

def china_line1(aa,tp) -> Line:
    keys = list(aa.keys())
    c = (
        Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            # update_time       total_confirm       today_confirm
            # total_heal        today_heal          total_nowconfirm
            # today_nowconfirm  total_dead          today_dead
            # total_suspect     today_suspect

        .add_xaxis(list(aa[keys[0]].values()))
        .add_yaxis(gt[keys[1]],list(aa[keys[1]].values()),is_selected=True,)
         #.add_yaxis(keys[2],list(aa[keys[2]].values()),is_selected=True,)
        .add_yaxis(gt[keys[3]],list(aa[keys[3]].values()),is_selected=True,)
         #.add_yaxis(keys[4],list(aa[keys[4]].values()),is_selected=False,)
        .add_yaxis(gt[keys[5]],list(aa[keys[5]].values()),is_selected=True,)
         #.add_yaxis(keys[6],list(aa[keys[6]].values()),is_selected=False,)
        .add_yaxis(gt[keys[7]],list(aa[keys[7]].values()),is_selected=True,)
         #.add_yaxis(keys[8],list(aa[keys[8]].values()),is_selected=False,)
        .add_yaxis(gt[keys[9]],list(aa[keys[9]].values()),is_selected=False,)
         #.add_yaxis(keys[10],list(aa[keys[10]].values()),is_selected=True,)

        #.add_yaxis('商家B',[57, 134, 137, 129, 145, 60, 49])
        .set_series_opts(linestyle_opts = opts.LineStyleOpts(width = 5,))
        .set_global_opts(title_opts=opts.TitleOpts(title="中国新冠疫情演变视图-累计\n"),
                         legend_opts=opts.LegendOpts(type_ = 'scroll', pos_bottom ='85%',),
                         datazoom_opts=opts.DataZoomOpts(is_show= True,  is_realtime=True, range_start=0,range_end=100,),)
    )
    c.render(path="D:\\javaweb\\line1.html")
    return c.dump_options_with_quotes()








aa = []
def china_pie(aa,tp) -> Pie:
    c=(
        Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add("",aa,
                 radius=["40%", "75%"],
                 label_opts=opts.LabelOpts(is_show=False),)
            .set_global_opts(
            title_opts=opts.TitleOpts(title="中国"+tp['usebase']+"饼状图"),
            legend_opts=opts.LegendOpts(is_show=True,type_ = 'scroll',pos_bottom = '0%',
                                        orient = 'horizontal',),
        )
            .set_series_opts()
    )
    c.render(path="D:\\javaweb\\pie.html")
    return c.dump_options_with_quotes()


def china_line2(aa,tp) -> Line:
    keys = list(aa.keys())
    c = (
        Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            #[update_time       total_confirm       today_confirm
            # total_heal        today_heal          total_nowconfirm
            # today_nowconfirm  total_dead          today_dead
            # total_suspect     today_suspect

            .add_xaxis(list(aa[keys[0]].values()))
            #.add_yaxis(keys[1],list(aa[keys[1]].values()),is_selected=True,)
            .add_yaxis(gt[keys[2]],list(aa[keys[2]].values()),is_selected=True,)
            #.add_yaxis(keys[3],list(aa[keys[3]].values()),is_selected=True,)
            .add_yaxis(gt[keys[4]],list(aa[keys[4]].values()),is_selected=True,)
            #.add_yaxis(keys[5],list(aa[keys[5]].values()),is_selected=True,)
            .add_yaxis(gt[keys[6]],list(aa[keys[6]].values()),is_selected=False,)
            #.add_yaxis(keys[7],list(aa[keys[7]].values()),is_selected=True,)
            .add_yaxis(gt[keys[8]],list(aa[keys[8]].values()),is_selected=True,)
            #.add_yaxis(keys[9],list(aa[keys[9]].values()),is_selected=False,)
            .add_yaxis(gt[keys[10]],list(aa[keys[10]].values()),is_selected=True,)
            #.add_yaxis('商家B',[57, 134, 137, 129, 145, 60, 49])
            .set_series_opts(linestyle_opts = opts.LineStyleOpts(width = 5,))
            .set_global_opts(title_opts=opts.TitleOpts(title="中国新冠疫情演变视图-每天\n"),
                             legend_opts=opts.LegendOpts(type_ = 'scroll', pos_bottom ='85%',),
                             datazoom_opts=opts.DataZoomOpts(is_show= True,  is_realtime=True,
                                                             range_start=0,range_end=100,),)
    )
    c.render(path="D:\\javaweb\\line2.html")
    return c.dump_options_with_quotes()


def map_base(pro) -> Map:
    # c = (
    #     Bar()
    #         .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    #         .add_yaxis("商家A", [randrange(0, 100) for _ in range(6)])
    #         .add_yaxis("商家B", [randrange(0, 100) for _ in range(6)])
    #         .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    #         .dump_options_with_quotes()
    # )
    # return c

    c = (
        Map()
            # .add_schema(maptype="china")
            .add("感染总人数", [[i.name, i.total_confirm] for i in pro],
                 is_selected=True,
                 maptype='china',
                 # type_="effectScatter",
                 # symbol_size=15,
                 )
            .set_global_opts(
            visualmap_opts=opts.VisualMapOpts(),  # 设置legend显示的最大值
            title_opts=opts.TitleOpts(title="中国疫情实况"), )
        # .dump_options_with_quotes(),

    )
    return c
"""
            '''.add("现在感染人数", [[i.name,i.total_nowConfirm] for i in pro],
                 is_selected=False,
                 type_="effectScatter",
                 symbol_size=15,
                 )
            .add("今日感染人数", [[i.name,i.today_confirm] for i in pro],
                 is_selected=False,
                 type_="effectScatter",
                 symbol_size=15,
                 )
            .add("总共治愈人数", [[i.name,i.total_heal] for i in pro],
                 is_selected=False,
                 type_="effectScatter",
                 symbol_size=15,
                 )
            .add("总共治愈人数", [[i.name,i.total_heal] for i in pro],
                 is_selected=False,
                 type_="effectScatter",
                 symbol_size=10,
                 )'''
"""